package de.lang.rollo.controller;

import java.net.UnknownHostException;
import java.util.Iterator;










import org.bson.types.ObjectId;
import org.mongojack.DBCursor;
import org.mongojack.DBRef;
import org.mongojack.JacksonDBCollection;
import org.mongojack.WriteResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.mongodb.DB;
import com.mongodb.MongoClient;
import com.pi4j.io.gpio.Pin;
import com.pi4j.io.gpio.RaspiPin;

import de.lang.rollo.controller.enums.EventType;
import de.lang.rollo.controller.enums.MotorState;
import de.lang.rollo.controller.enums.SchalterType;
import de.lang.rollo.controller.managed.MongoManaged;
import de.lang.rollo.controller.model.RolloEvent;
import de.lang.rollo.controller.model.RolloMotor;
import de.lang.rollo.controller.model.Schalter;
import de.lang.rollo.controller.model.daos.SchalterDAO;


/**
 * Hello world!
 * 
 */

public class App {
	private static Logger logger = LoggerFactory.getLogger(App.class);

	static MongoClient mongoClient;
	static DB db;

	static JacksonDBCollection<RolloEvent, String> events;
	static JacksonDBCollection<RolloMotor, String> motoren;
	static JacksonDBCollection<Schalter, String> schalter;

	public static void main(String[] args) {

		try {
			MongoManaged mongo = new MongoManaged("localhost", 27017, "rolloTimer");

			events = mongo.getEvents();
			motoren = mongo.getMotoren();
			schalter = mongo.getSchalter();

			// sList.add(sch);
			
			
			add();
			read();
			mongo.stop();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
		}

	}

	public static void read() {
		SchalterDAO sDao = new SchalterDAO();
		 DBCursor<RolloMotor> it = motoren.find().is("name", "Motor 2");
		
		while(it.hasNext()){
			RolloMotor m = it.next();
			System.out.println("Motor: " + m.id + " - " + m.name);
		}
		
		Iterator<Schalter> it2 = sDao.getAllSchalter().iterator();
		
		while(it2.hasNext()){
			Schalter s = it2.next();
			RolloMotor asd = s.getMotor();

			System.out.println("Schalter: " + s.getId() + " - " + s.name + " Motor: " + asd.getId() + " / " + asd.getName()+ " / " + asd.getRunTimeUp());
		}
		
		 RolloMotor m = motoren.findOne();
		 m.setName("Lala");
		 motoren.save(m);
		 
		 it = motoren.find();
			
			while(it.hasNext()){
				RolloMotor f = it.next();
				System.out.println("Motor: " + f.id + " - " + f.name);
			}
		 
		 it2 = sDao.getAllSchalter().iterator();
			
			while(it2.hasNext()){
				Schalter s = it2.next();
				System.out.println("Schalter: " + s.getId() + " - " + s.name + " Motor: " + s.getMotor().getId() + " / " + s.getMotor().getName()+ " / " + s.getMotor().getRunTimeUp());
			}
		
		DBCursor<RolloEvent> it3 = events.find();
		
		while(it3.hasNext()){
			RolloEvent e = it3.next();
			System.out.println("Event: " + e.getId() + " - " + e.getEventType() + " Motor: " + e.getMotor().id + " / " + e.getMotor().getName() + " / " + e.getRolloType());
		}
	}

	public static void add() {
		// DB
		
		SchalterDAO schalterDao = new SchalterDAO();

		events.drop();
		motoren.drop();
		schalter.drop();
		RolloMotor motor = null;
		motor = new RolloMotor();
		motor.name = ("Motor 2");
		motor.state = (MotorState.UP);
		motor.runTimeUp = 1000;
//		motor.id = ObjectId.get().toString();

		// motor._id = "lala";
//		 mList.add(motor);
//		motoren.save(motor);
//		WriteResult<RolloMotor, String> result = motoren.insert(motor);
//		RolloMotor found = motoren.findOneById(result.getSavedId());
//		System.out.println("Motor:" + found.getName() + " / " + found.getId());

		Schalter sch = new Schalter();
		sch.name = (motor.name + "_switch");
		sch.setPin(RaspiPin.GPIO_00);
		// sch.setType(SchalterType.UP);
		sch.type = (SchalterType.DOWN);
		sch.setMotor(motor);
		schalterDao.insertSchalter(sch);
		// sch._id = "adsd";
//		schalter.save(sch);
//		sch.setId(null);
		logger.info("s1: " + sch.getId());
		sch.name = "bamboo_sw";
		schalterDao.updateSchalter(sch);
		logger.info("s2: " + sch.getId());
		
		RolloEvent ev = new RolloEvent();
		ev.setEventType(EventType.DOWN);
		ev.setMotor(motor);
		// WriteResult<Schalter, Long> result = schalter.insert(sch);
		// long id = result.getSavedId();
		// Schalter s = schalter.findOneById(id);
//		Schalter s = schalter.findOne();
//		System.out.println("Schalter: " + s.name + " für Motor: " + s.motor.id
//				+ "  ll  " + sch.getPin().getAddress());
		// List<Schalter> list = schalter.findOne()
		// for (Schalter s: list){
		// System.out.println("Schalter: " +s.name + " für Motor: " +
		// s.getId());
		// }

	}
}
